<?php

namespace App\Http\Controllers\Admin;

use App\Model\Permission;
use App\Model\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class RoleController extends Controller
{
    public function auth($id)
    {
        // 获取当前角色
        $role = Role::find($id);

        // 获取所有的权限列表
        $perms = Permission::get();

        // 获取当前角色的权限
        $own_perms = $role->permission;

        $own_pers = [];
        // 如果没有数据给一个空数组
        if(empty($own_perms)){
            $own_pers = [];
        }else{
            foreach ($own_perms as $v){
                $own_pers[] = $v->id;
            }
        }

        return view('admin.role.auth',compact('role','perms','own_pers'));
    }

    // 授权
    public function doAuth(Request $request)
    {
        $input = $request->except('_token');

        // 先删除角色权限表
        DB::table('role_permission')->where(['role_id' => $input['role_id']])->delete();

        // 在追家相对应的权限
        if(!empty($input['permission_id'])){
            foreach ($input['permission_id'] as $v){
                $result = DB::table('role_permission')->insert(['role_id'=>$input['role_id'],'permission_id'=>$v]);
            }
        }else{
            $data = [
                'code'=> 1,
                'message'=>'授权成功'
            ];
            return $data;
        }

        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'授权失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'授权成功'
            ];
        }
        return $data;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $rolelist = Role::orderBy('id','asc')
            ->where(function($query) use($request){
                $role_name= $request->input('role_name');
                if(!empty($role_name)){
                    $query->where('role_name','like','%'. $role_name .'%');
                }
            })
            ->paginate($request->input('num') ? $request->input('num') : 10);

        return view('admin.role.list',compact('rolelist','request'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.role.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // 1.获取用户添加的数据  role_name
        $input = $request->all();

        $role_name = Role::where('role_name',$input['role_name'])->first();

        if($role_name){
            $data = [
                'code'=> 0,
                'message'=>'该角色已经存在'
            ];
            return $data;
        }

        // 封装插入数据库的数组
        $data = [
            'role_name' => $input['role_name'],
        ];

        // 验证
        $result = Role::create($data);

        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'添加失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'添加成功'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = Role::find($id);
        return view('admin.role.edit',compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // 查询
        $role = Role::find($id);

        // 获取修改的数据
        $role_name = $request->input('role_name');

        $data= [
            'role_name' => $role_name,
        ];

        $result = $role->update($data);
        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'修改失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'修改成功'
            ];
        }
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $user = Role::find($id);

        $result = $user->delete();

        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'删除失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'删除成功'
            ];
        }

        return $data;
    }

    // 删除选中id
    public function delAll(Request $request)
    {
        $ids = $request->input('ids');

        $result = Role::destroy($ids);

        if($result === FALSE){
            $data = [
                'code'=> 0,
                'message'=>'删除失败'
            ];
        }else{
            $data = [
                'code'=> 1,
                'message'=>'删除成功'
            ];
        }

        return $data;

    }
}
